﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace time
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public string path = Directory.GetCurrentDirectory() + @"\data.txt";

       
        private void btn_update_Click(object sender, EventArgs e)//更新listview1.
        {
            try
            {
                DateTime mydate = DateTime.Now;//日期
                DateTime worktime_start = start_time.Value;
                DateTime worktime_end = end_time.Value;
                
                    TimeSpan worktime = worktime_end.Subtract(worktime_start);//计算全天工时
                    TimeSpan overtime = worktime.Add(TimeSpan.FromHours(-8));//计算全天加班工时
                    ListViewItem myitem = new ListViewItem();

                    myitem.Text = (listView1.Items.Count + 1).ToString();//序号:当前的总行数+1
                    myitem.SubItems.Add(mydate.ToShortDateString());//日期
                    myitem.SubItems.Add(worktime_start.ToShortTimeString());//上班时间
                    myitem.SubItems.Add(worktime_end.ToShortTimeString());//下班时间
                    myitem.SubItems.Add(worktime.ToString());//全天工时
                    myitem.SubItems.Add(overtime.ToString());//全天加班时间
                    listView1.Items.Add(myitem);
                
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message.ToString(), "提示");
            }
            changeColumenWidth();
            

        }
        private void changeColumenWidth()//初始化listview1的各项,并改变各项的宽度.
        {
            for (int i = 0; i < listView1.Columns.Count; i++)
            {
                listView1.Columns[i].Width = -2;//-1:根据内容,-2:根据项
            }
        }
        private void listview_Columns()//初始化listview1控件.
        {
            listView1.Columns.Add("序号", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("日期", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("上班时间", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("下班时间", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("全天工时", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("全天加班工时", 100, HorizontalAlignment.Left);
        }
        private void mySort()//重排序号
        {
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                listView1.Items[i].Text = (i + 1).ToString();
            }
        }
        private void readData()//获取data.txt,若不存在则创建.
        {
            if (!File.Exists(path))
            {
                File.Create(path);
            }
            else //读取并显示到listview1
            {
                StreamReader sr = new StreamReader(path);
                string tempData;
                while (!sr.EndOfStream)
                {
                    ListViewItem myItem = new ListViewItem();
                    tempData = sr.ReadLine().Trim(); ;
                    string[] myDta = tempData.Split(' ');
                    myItem.Text = myDta[0];
                    myItem.SubItems.Add(myDta[1]);
                    myItem.SubItems.Add(myDta[2]);
                    myItem.SubItems.Add(myDta[3]);
                    myItem.SubItems.Add(myDta[4]);
                    myItem.SubItems.Add(myDta[5]);
                    listView1.Items.Add(myItem);
                }
                sr.Close();
            }
        }

        private string GetDayName()
        {
            string result = "";
            DateTime today = DateTime.Now;
            if (today.DayOfWeek == DayOfWeek.Monday)
            {
                result = "星期一";
            }
            else if (today.DayOfWeek == DayOfWeek.Tuesday)
            {
                result = "星期二";
            }
            else if (today.DayOfWeek == DayOfWeek.Wednesday)
            {
                result = "星期三";
            }
            else if (today.DayOfWeek == DayOfWeek.Thursday)
            {
                result = "星期四";
            }
            else if (today.DayOfWeek == DayOfWeek.Friday)
            {
                result = "星期五";
            }
            else if (today.DayOfWeek == DayOfWeek.Saturday)
            {
                result = "星期六";
            }
            else if (today.DayOfWeek == DayOfWeek.Sunday)
            {
                result = "星期日";
            }
            return result;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "当前时间:" + DateTime.Now.ToString() + " " + GetDayName();
            readData();
            listview_Columns();
            changeColumenWidth();//设置listview项的宽度.
        }
        private void btn_output_Click(object sender, EventArgs e)//导出数据
        {
            StreamWriter sw = new StreamWriter("data.txt");
            foreach (ListViewItem item in listView1.Items)
            {
                // string s = item.Text;
                string s = "";
                foreach (ListViewItem.ListViewSubItem subitem in item.SubItems)
                {
                    s = s + " " + subitem.Text;
                }
                sw.Write(s);
                sw.WriteLine("");
            }
            sw.Close();
            MessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private void btn_delete_Click(object sender, EventArgs e)//删除选中行
        {
            if (listView1.SelectedItems.Count > 0)
            {
                foreach (ListViewItem item in listView1.SelectedItems)
                {
                    item.Remove();
                }
                mySort();
            }
            else
            {
                MessageBox.Show("无选中项!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            toolStripStatusLabel1.Text = "当前时间:" + DateTime.Now.ToString() + " " + GetDayName();
        }
        private string GetOverTime()//根据选择的行计算加班总时间(小时)
        {
            try
            {

                string[] str = new string[listView1.SelectedItems.Count];//选取的行数
                string[] mystr = new string[str.Length];
                double hr = 0;
                double mi = 0;
                double sum = 0;

                for (int i = 0; i < listView1.SelectedItems.Count; i++)
                {
                    str[i] = listView1.SelectedItems[i].SubItems[5].Text;//从选择的第一行开始计算
                    if (str[i].StartsWith("-"))
                    {
                        mystr[i] = str[i].Substring(0, 3) + "-" + str[i].Substring(4, 2);
                    }
                    else
                    {
                        mystr[i] = str[i].Substring(0, 2) + str[i].Substring(3, 2);
                    }
                }

                foreach (string x in mystr)
                {
                    if (x.StartsWith("-"))
                    {
                        hr += double.Parse(x.Substring(0, 3));
                        mi += double.Parse(x.Substring(3, 3));
                    }
                    else
                    {
                        hr += double.Parse(x.Substring(0, 2));
                        mi += double.Parse(x.Substring(2, 2));
                    }
                    sum = (hr * 60 + mi) / 60;
                }
                
                    return sum.ToString("0.0小时");            
            }
            catch (Exception ee)
            {
                //MessageBox.Show(ee.Message.ToString());
                return ee.Message.ToString();
                
                
            }
               
        }
        private void button1_Click(object sender, EventArgs e)//计算加班总时间(小时)
        {
            MessageBox.Show(start_time.Value.ToString());
            
           
        }

        private void 计算加班时间ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(GetOverTime().ToString());
        }
    }
}

